#include "chatmessagewidget.h"
#include <QLabel>
#include <QHBoxLayout>

ChatMessageWidget::ChatMessageWidget(QWidget *parent, const QString &message, bool isAI)
    : m_isAI(isAI)
{
    setupUI(message);
}

void ChatMessageWidget::setupUI(const QString &message)
{
    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->setContentsMargins(10, 5, 10, 5);
    mainLayout->setSpacing(10);

    // 头像
    QLabel *avatar = new QLabel();
    avatar->setFixedSize(40, 40);
    avatar->setStyleSheet(
        QString("QLabel {"
                "border-radius: 20px;"
                "background-color: %1;"
                "color: white;"
                "font-weight: bold;"
                "}").arg(m_isAI ? "#6c757d" : "#007bff")
    );
    avatar->setText(m_isAI ? "AI" : "You");
    avatar->setAlignment(Qt::AlignCenter);

    // 消息内容
    QLabel *messageLabel = new QLabel(message);
    messageLabel->setWordWrap(true);
    messageLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
    messageLabel->setMaximumWidth(350);
    messageLabel->setMargin(10);

    // 设置样式
    QString labelStyle = QString(
        "QLabel {"
        "background-color: %1;"
        "border: 1px solid %2;"
        "border-radius: 10px;"
        "padding: 8px;"
        "font-size: 14px;"
        "}"
    ).arg(m_isAI ? "#f8f9fa" : "#e3f2fd")
     .arg(m_isAI ? "#dee2e6" : "#bbdefb");

    messageLabel->setStyleSheet(labelStyle);

    if (m_isAI) {
        mainLayout->addWidget(avatar);
        mainLayout->addWidget(messageLabel);
        mainLayout->addStretch();
    } else {
        mainLayout->addStretch();
        mainLayout->addWidget(messageLabel);
        mainLayout->addWidget(avatar);
    }
}
